package service

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto/rpc"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetEpisodeList(c *gin.Context) {
	var form request.GetEpisodeList
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	episode, err := config.VideoRpcClient.GetEpisodeList(c, &__.GetEpisodeListReq{
		VideoId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "返回分集列表数据失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "返回分集列表数据,记录播放进度成功", "data": gin.H{
		"list": episode.List,
	}})
	return
}

func CreateEpisodeLog(c *gin.Context) {
	var form request.CreateEpisodeLog
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	episode, err := config.VideoRpcClient.CreateEpisodeLog(c, &__.CreateEpisodeLogReq{
		UserId:    int64(form.UserId),
		VideoId:   int64(form.VideoId),
		EpisodeId: int64(form.EpisodeId),
		Duration:  int64(form.Duration),
		LookTime:  int64(form.LookTime),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "自动观看记录失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "自动观看记录成功", "data": gin.H{
		"list": episode.LogId,
	}})
	return
}

func GetEpisodeLog(c *gin.Context) {
	var form request.GetEpisodeLog
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	episode, err := config.VideoRpcClient.GetEpisodeLog(c, &__.GetEpisodeLogReq{
		LogId: int64(form.LogId),
		Page:  int64(form.Page),
		Size:  int64(form.Size),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "查询观看历史失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询观看历史成功", "data": gin.H{
		"list": episode.List,
	}})
	return
}
